var ctx=window.daijia.ctx;

$(document).ready(function(){
	
	$("#start").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#end").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	$('#companyIds').multiselect({
		enableFiltering: true,
	    includeSelectAllOption: true,
	    selectAllText: '全部',
	    selectAllJustVisible: true,
	    buttonWidth: '100%',
	    nonSelectedText: '请选择所属公司',
	    nSelectedText: ' - 已选择',
	    numberDisplayed: 4,
	    allSelectedText: '已选择全部'
	});
	
	
	$("input[name=rewardType]").change(function(){
		$("#guding_div").hide();
		$("#percent_div").hide();
		
		if($(this).val()=="guding"){
			$("#guding_div").fadeIn(200);
		}
		else if($(this).val()=="percent"){
			$("#percent_div").fadeIn(200);
		}
	});
	
	$("#submit_button").click(function(){
		
		if(main){
			if(null==$("#companyIds").val()){
				$.scojs_message("请选择所属公司", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		if($("#orderType").val()==""){
			$.scojs_message("请选择订单类型", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#finishAmounts").val()==""){
			$.scojs_message("请填写完成单量", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#lowestMoney").val()==""){
			$.scojs_message("请填写订单金额", $.scojs_message.TYPE_ERROR);
			return false;
		}
		
		if($("#start").val()!="" && $("#end").val()!=""){
			if($("#start").val()>$("#end").val()){
				 $.scojs_message("起始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
				 return false;
			}
		}
		
		if($("input[name=rewardType]:checked").val()=="guding"){
			if($("#guding").val()==""){
				$.scojs_message("请填写返现金额", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		else if($("input[name=rewardType]:checked").val()=="percent"){
			if($("#percent").val()==""){
				$.scojs_message("请填写返现比例", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		$("#submit_button").attr("disabled",true);
		$.get(ctx+'/driver/getRewardDrivers',$("#submit_form").serialize(),function(data){
			$("#submit_button").attr("disabled",false);
			if(null!=data && data.length>0){
				$("#number").html(data.length);
         		var btnhtml="";
         		$(data).each(function(index,element){
         			btnhtml+="<div class='col-xs-3 rewardDrivers' style='margin-top:20px;' driverId='"+element.driverId+"' driverMoney='"+element.rewardMoney+"' ><span class='label label-info' style='font-size:15px;'>"+element.driverRealName+"("+element.rewardMoney+"元)&ensp;<span class='glyphicon glyphicon-remove removedriver' style='color:red;'></span></span></div>";
         		});
     			$("#driver_btn").html('');
     			$("#driver_btn").append(btnhtml);
     			$("#errormessage_div").html('');
     			$("#reward_process .progress-bar").css("width","1%");
				$("#reward_process .progress-bar span").html("0%已完成");
				$("#reward_process").hide();
				$("#save").attr("disabled",false);
     			
         		$("#dialog_driver").modal();
			}
			else{
				$.scojs_message("没有满足条件的服务人员", $.scojs_message.TYPE_ERROR);
			}
		});
		
	});
	
	//modal
	$("#cancel").click(function(){
		$("#dialog_driver").modal("hide");
	});
	
	$(document).on('click', '.removedriver', function() {
		$(this).parent().parent().remove();
		$("#number").html(parseInt($("#number").text())-1);
		if($("#number").text()==0){
			$("#cancel").click();
		}
	});
	
	$("#save").click(function(){
		var driverIds=[];
		var moneys=[];
		$(".rewardDrivers").each(function(index,element){
			driverIds.push($(element).attr("driverId"));
			moneys.push($(element).attr("driverMoney"));
		});
		
		var memo="条件为：";
		memo+="至少完成"+$("#finishAmounts").val()+"单，每单金额不少于"+$("#lowestMoney").val()+"元";
		if($("#start").val()!="" || $("#end").val()!=""){
			if($("#start").val()!="" && $("#end").val()!=""){
				memo+="，并且完成时间在"+$("#start").val()+"到"+$("#end").val()+"之间";
			}
			else{
				if($("#start").val()!=""){
					memo+="，并且完成时间大于"+$("#start").val();
				}
				else if($("#end").val()!=""){
					memo+="，并且完成时间小于"+$("#end").val();
				}
			}
		}
		memo+="的"+$("#orderType option:selected").text()+"订单，";
		if($("input[name=rewardType]:checked").val()=="guding"){
			memo+="奖励方式为固定金额："+$("#guding").val()+"元。";
		}
		else if($("input[name=rewardType]:checked").val()=="percent"){
			memo+="奖励方式为百分比金额：百分之"+$("#percent").val()+"。";
		}
		
		$("#save").attr("disabled",true);
		$.post(ctx+'/driver/driverReward',{"drivers":driverIds.join(","),"money":moneys.join(","),"memo":memo},function(data){
			if(t){
				clearInterval(t);
				$("#reward_process .progress-bar").css("width","100%");
				$("#reward_process .progress-bar span").html("100%已完成");
			}
			if(data.success){
				
				$.scojs_message("操作成功", $.scojs_message.TYPE_OK);
				
				if(null!=data.errorMessage && data.errorMessage!=""){
					$("#errormessage_div").html(data.errorMessage);
				}
			}
			else{
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
			}
		});
		
		
		$("#reward_process").show();
		$("#reward_process .progress-bar").css("width","1%");
		$("#reward_process .progress-bar span").html("0%已完成");
		var t=setInterval(function(){
			$.get(ctx+'/driver/getRewardProcess',{},function(data){
				if(null!=data && data!=""){
					$("#reward_process .progress-bar").css("width",parseFloat(data)*100+"%");
					$("#reward_process .progress-bar span").html(parseFloat(data).toFixed(1)*100+"%已完成");
				}
			});
		},1000);
		
	});
	
});

